/*
 * 人人信用管理有限公司版权所有.
 * 
 * @Description:
 * 
 * @author: liyuliang
 * 
 * @date: 20 Apr 2017 10:44:16 am
 * 
 * 注意：本内容仅限于人人信用管理有限公司内部传阅，禁止外泄以及用于其他的商业目的.
 */

package com.rrx360.common.web.util;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.rrx360.common.config.global.GlobalConfig;
import com.rrx360.common.util.JsonUtil;

public class ResourceFunctions {
  /** web容器的上下文 */
  private static String context;
  /** 页面资源的上下文 */
  private static String ctx;

  private static String realRoot;

  private static String jsRoot;

  private static String cssRoot;

  private static String vendorRoot;

  private static String imgRoot;

  private static boolean isComp;

  private static boolean isMerge;

  private static boolean isLess;

  /**
   * cache rk frame fragment flag
   */
  private static boolean isFragmentCache;

  private static String ctxKey = "app.ctx";

  private static Map<String, Map<String, String[]>> configMap =
      new HashMap<String, Map<String, String[]>>();

  static {
    init();
  }

  public void setCtxKey(String ctxKey) {
    ResourceFunctions.ctxKey = ctxKey;
  }

  public static void init() {
    isComp = "Y".equals(GlobalConfig.get("file.compMode"));
    isMerge = "Y".equals(GlobalConfig.get("file.mergeMode"));
    isFragmentCache = "Y".equals(GlobalConfig.get("fragment.cache"));
    isLess = !"N".equals(GlobalConfig.get("css.lessMode"));

    configMap.clear();
    URL url = ResourceFunctions.class.getClassLoader().getResource("config/jsCssCfg.json");
    if (url == null) {
      return;
    }
    File file = new File(url.getFile());
    Map<String, Object> content = JsonUtil.parse(file);
    for (Map.Entry<String, Object> entry : content.entrySet()) {
      String name = entry.getKey();
      Map<String, String[]> m = new HashMap<String, String[]>();
      configMap.put(name, m);
      Map<String, Object> valueMap = (Map<String, Object>) entry.getValue();
      Object cfgNormal = valueMap.get("normal");
      Object cfgMin = valueMap.get("min");
      Object merge = valueMap.get("merge");
      Object cfgMNormal = null;
      Object cfgMMin = null;
      if (merge != null) {
        cfgMNormal = ((Map<String, Object>) merge).get("normal");
        cfgMMin = ((Map<String, Object>) merge).get("min");
      }

      String[] normal = getValue(cfgNormal, cfgMin, cfgMNormal, cfgMMin);
      String[] min = getValue(cfgMin, cfgNormal, cfgMMin, cfgMNormal);
      String[] mergeNormal = getValue(cfgMNormal, cfgMMin, cfgNormal, cfgMin);
      String[] mergeMin = getValue(cfgMMin, cfgMNormal, cfgMin, cfgNormal);
      m.put("normal", normal);
      m.put("min", min);
      m.put("mnormal", mergeNormal);
      m.put("mmin", mergeMin);
    }

  }

  public static boolean isMerge() {
    return isMerge;
  }

  public static boolean isComp() {
    return isComp;
  }

  public static boolean isFragmentCache() {
    return isFragmentCache;
  }

  public static boolean isLess() {
    return isLess;
  }


  private static String[] getValue(Object key, Object o1, Object o2, Object o3) {
    Object value = key != null ? key : (o1 != null ? o1 : (o2 != null ? o2 : o3));
    if (value instanceof String[]) {
      return (String[]) value;
    }
    if (value instanceof String) {
      return new String[] {(String) value};
    }
    JSONArray array = (JSONArray) value;
    return Arrays.asList(array.toArray()).toArray(new String[] {});
  }

  public static String getRealRoot() {
    return realRoot;
  }

  public static void setRealRoot(String realRoot) {
    ResourceFunctions.realRoot = realRoot;
  }

  public static void setContext(String context) {
    ResourceFunctions.context = context;
  }

  public static String getContext() {
    return context;
  }

  public static String getCtx() {
    if (ctx != null) {
      return ctx;
    }
    ctx = GlobalConfig.get(ctxKey);
    if (ctx == null || ctx.equals("$CONTEXT")) {
      ctx = context;
    }
    return ctx;
  }

  private static String simpleCtx;

  public static String getCookiePath() {
    return getSimpleCtx() + "/";
  }

  public static String getSimpleCtx() {
    if (simpleCtx != null) {
      return simpleCtx;
    }
    String fullCtx = getCtx();
    if (!fullCtx.startsWith("http")) {
      return fullCtx;
    }

    try {
      URL url = new URL(fullCtx);
      simpleCtx = url.getPath();

      return simpleCtx;
    } catch (MalformedURLException e) {
      throw new RuntimeException(e);
    }
  }

  public static String[] getPathByName(String name) {
    String key = isComp ? "min" : "normal";
    if (isMerge) {
      key = "m" + key;
    }
    return configMap.get(name).get(key);
  }

  public static String getJsRoot() {
    if (jsRoot != null) {
      return jsRoot;
    }
    jsRoot = getCtx() + "/static/js";
//    if (isComp) {
//      jsRoot = getCtx() + "/static/js";
//    } else {
//      jsRoot = getCtx() + "/static/js";
//    }
    return jsRoot;
  }

  public static String getCssRoot() {
    if (cssRoot != null) {
      return cssRoot;
    }
    cssRoot = getCtx() + "/static/css";
//    if (isComp) {
//      cssRoot = getCtx() + "/static/css";
//    } else {
//      cssRoot = getCtx() + "/static/css";
//    }
    return cssRoot;
  }

  public static String getStaticRoot() {
    return getCtx() + "/static";
  }

  public static String getImgRoot() {
    if (imgRoot != null) {
      return imgRoot;
    }
    return getCtx() + "/static/img";
  }

  public static String getVendorRoot() {
    if (vendorRoot != null) {
      return vendorRoot;
    }
    return getCtx() + "/static/vendor";
  }

  public static String getRealPath(String path) {
    path = path.substring(getCtx().length());
    return realRoot + path.replaceFirst("static", "public");
  }
}
